const http = require("http");
const server = http.createServer();
const urlModule = require("url");
server.on("request", (req, res) => {
  // res.end("success(123)");
  const { pathname: url, query } = urlModule.parse(req.url, true);
  // 如果前端请求的是/api/jsonp的话对应用jsonp处理
  const person = {
    name: "imycode",
    age: 22,
    gender: "男",
  };

  if (url === "/api/jsonp") {
    const str = `${query.callback}(${JSON.stringify(person)})`;
    res.end(str);
  } else {
    res.end("404");
  }
});
server.listen(3000, () => {
  console.log("http://127.0.0.1:3000");
});
